package com.chuangsi.project.admin.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangsi.common.exception.ServiceException;
import com.chuangsi.framework.web.controller.BaseController;
import com.chuangsi.framework.web.domain.ResultObject;
import com.chuangsi.framework.web.page.TableDataInfo;
import com.chuangsi.project.admin.domain.Brand;
import com.chuangsi.project.admin.dto.admin.brand.*;
import com.chuangsi.project.admin.dto.common.SelectDto;
import com.chuangsi.project.admin.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangshengming
 * @date 2023/3/23
 */
@Api(tags = "品牌管理")
@RestController
@RequestMapping("/adminApi/brand")
public class BrandController  extends BaseController {

    @Autowired
    private BrandService brandService;

    @ApiOperation(value = "品牌分页",notes = "王圣铭")
    @GetMapping("/brandPage")
    public TableDataInfo<ReqBrandUpdateDto> brandPage(ReqBrandPageDto req){
        List<ResBrandPageDto> page = brandService.brandPage(req);
        return getDataTable(page);
    }

    @ApiOperation(value = "新增品牌",notes = "王圣铭")
    @PostMapping("/brandAdd")
    public ResultObject<Void> brandAdd(@RequestBody @Valid ReqBrandAddDto req){
        Brand brand = new Brand();
        brand.setBrandName(req.getBrandName());
        boolean flag = brandService.save(brand);
        if(flag){return ResultObject.ok("新增成功");}
        else {return ResultObject.error("新增失败");}
    }

    @ApiOperation(value = "修改品牌",notes = "王圣铭")
    @PostMapping("/brandUpdate")
    public ResultObject<Void> brandUpdate(@RequestBody @Valid ReqBrandUpdateDto req){
        Brand brand = brandService.getById(req.getBrandId());
        if(brand==null){
            throw new ServiceException("未查询到品牌详情");
        }
        brand.setBrandName(req.getBrandName());
        boolean flag = brandService.updateById(brand);
        if(flag){return ResultObject.ok("新增成功");}
        else {return ResultObject.error("新增失败");}
    }

    @ApiOperation(value = "品牌详情",notes = "王圣铭")
    @PostMapping("/brandDetail")
    public ResultObject<ReqBrandUpdateDto> brandDetail(@RequestBody @Valid ReqBrandDetailDto req){
        Brand brand = brandService.getById(req.getBrandId());
        if(brand==null){
            throw new ServiceException("未查询到品牌详情");
        }
        ReqBrandUpdateDto res = new ReqBrandUpdateDto();
        res.setBrandId(brand.getBrandId());
        res.setBrandName(brand.getBrandName());
        return ResultObject.ok("查询成功").setData(res);
    }

    @ApiOperation(value = "品牌删除",notes = "王圣铭")
    @PostMapping("/brandDel")
    public ResultObject<Void> brandDel(@RequestBody @Valid ReqBrandDetailDto req){
        brandService.removeById(req.getBrandId());
        return ResultObject.ok("删除成功");
    }

    @ApiOperation(value = "品牌下拉选",notes = "王圣铭")
    @PostMapping("/brandSelect")
    public ResultObject<List<SelectDto>> brandSelect(){
        List<SelectDto> list = brandService.brandSelect();
        return ResultObject.ok("查询成功").setData(list);
    }


}
